﻿// ***********************************************************************
// Assembly         : GooglePredict
// Author           : aj
// Created          : 10-08-2010
//
// Last Modified By : aj
// Last Modified On : 10-08-2010
// Description      : 
//
//
// ***********************************************************************
using System.Collections;

namespace Google.API.Predict.Requests
{
    using Core;
    using Newtonsoft.Json.Linq;
    using Responses;

    /// <summary>
    /// Training Request Class
    /// http://code.google.com/apis/predict/docs/developer-guide.html#invoke-training
    /// </summary>
    public class TrainingRequest : GooglePredictionApiRequest<TrainingResponse>
    {
        /// <summary>
        /// Creates a new Training Request
        /// </summary>
        public TrainingRequest()
        {
            RequestFormat = "{0}?data={1}%2F{2}";
            Method = HttpMethod.Post;
            PostData = new System.Collections.ObjectModel.ReadOnlyCollection<byte>(System.Text.Encoding.ASCII.GetBytes("{\"data\":{}}"));
        }

        /// <summary>
        /// Executes the Training request
        /// Parses response and stores in "Response" field
        /// Determines success/fail by comparing response data to current model
        /// </summary>
        /// <returns>Parsed JSON response</returns>
        internal override JObject Execute()
        {
            var o = base.Execute();
            if (Response.Success)
            {
                Response.TrainingInvoked = Response.Data.ToUpperInvariant().Equals(Bucket.ToUpperInvariant() + "/" + Data.ToUpperInvariant());                    
            }

            return o;
        }
    }
}
